package com.jdym.back.controller;


import com.jdym.R;
import com.jdym.back.entity.Pinggu;
import com.jdym.back.service.PingguService;
import com.jdym.utils.UploadUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 评估表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@Api("评估管理")
@CrossOrigin
@RestController
@RequestMapping("/back/pinggu")
public class PingguController {

    @Autowired
    PingguService pingguService;

//    Pinggu selectPinggu(int cobankId);

    @GetMapping("/selectPinggu/{id}")
    public R selectPinggu(@PathVariable int id) {
        System.out.println("id:" + id);
        List<Pinggu> pinggu = pingguService.selectPinggu(id);
        System.out.println("=====评估表查询=======" + pinggu);
        return R.ok().data("pinggu", pinggu).message("评估信息查询");
    }

    @GetMapping("/selectPingguID/{id}")
    public R selectPingguID(@PathVariable int id) {
        System.out.println("id:" + id);
//添加的内容@RequestParam("pinggu_status") int pinggu_status
//        Pinggu pinggus = new Pinggu();
//        pinggus.setPinggu_status(pinggu_status);
//        pinggus.setPinguId(id);
//        pingguService.getBaseMapper().updateById(pinggus);
//

        Pinggu pinggus = pingguService.selectPingguID(id);

        System.out.println("=====评估表查询ID=======" + pinggus);
        return R.ok().data("pinggus", pinggus).message("评估信息查询id");
    }


    //    评估文件上传
    @RequestMapping("/pingguImage")
    public R pingguImage(@RequestParam("file") MultipartFile file) {
        System.out.println("file:" + file);
        System.out.println("评估文件上传========");

        String imagePath = UploadUtils.upload(file);
//        上传之后的文件保存在数据库中
        String url = "http://localhost:8010" + imagePath;//后台上传
        if (imagePath != null) {

            return R.ok().data("pingguImage", url);
        } else {
            return R.ok().data("pingguImage", "上传失败");
        }
    }

    //    评估文件上传到数据库中
    @RequestMapping("/updateImg/{id}")
    public R pingguImageURL(@RequestParam("imgUrl") String imgUrl,@PathVariable int id) {
        System.out.println("评估文件上传========");
        Pinggu pinggu = new Pinggu();
        pinggu.setPinggu_url_1(imgUrl);
        pinggu.setPinguId(id);
        System.out.println("插入数据上传文件1" + pinggu);
        pingguService.getBaseMapper().updateById(pinggu);
        return R.ok().data("msg", "上传成功");
    }

}

